package com.zhangj.alljvm.reference;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ReferenceQueueTest2
 * @Description
 * @Author zhangjie
 * @Date 2022/3/11 19:30
 * @Version 1.0
 */
public class ReferenceQueueTest2 {
    public static void main(String[] args) {

        Object obj = new Object();
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
        PhantomReference<Object> phantomReference = new PhantomReference<Object>(obj,referenceQueue);

        System.out.println("obj = " + obj);
        System.out.println("phantomReference.get() = " + phantomReference.get());
        System.out.println("referenceQueue.poll() = " + referenceQueue.poll());

        System.out.println("======================");
        obj = null;
        System.gc();
        try {
             TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
             e.printStackTrace();
        }

        System.out.println("obj = " + obj);
        System.out.println("phantomReference.get() = " + phantomReference.get());
        System.out.println("referenceQueue.poll() = " + referenceQueue.poll());

    }
}
